package com.example.controller;

import com.example.common.CaptureConfig;
import com.example.common.Result;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: PengFei.He
 * @Date: 2025/1/2 23:08
 * @Function:  验证码
 */
@RestController
@RequestMapping
@CrossOrigin
public class CaptureController {

    /**
     * 获取图形验证码接口,前后端分离方式
     * @param response
     * @param request
     */
    @RequestMapping("capture")
    public void capture(@RequestParam("key") String key, HttpServletResponse response, HttpServletRequest request) throws IOException {

        //法一：
//        SpecCaptcha captcha = new SpecCaptcha(135,33,5);
//        captcha.setCharType(Captcha.TYPE_NUM_AND_UPPER);
        // 把验证码存储起来：redis、全局Map等
//        CaptureConfig.CAPTURE_MAP.put(key,captcha.text().toLowerCase());
//        CaptchaUtil.out(captcha,request,response);

        //法二：加减法
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(135, 33);
        arithmeticCaptcha.setLen(4);
        arithmeticCaptcha.getArithmeticString(); //公式
        CaptureConfig.CAPTURE_MAP.put(key,arithmeticCaptcha.text().toLowerCase());
        CaptchaUtil.out(arithmeticCaptcha,request,response);
    }
}
